package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Reference
	UserService userService;

	/**
	 * 获取用户名
	 *
	 * @return
	 */
	@RequestMapping("/findUsername")
	@ResponseBody
	public Result findUsername(HttpServletRequest request) {
		try {
			//获取安全框架的上下文对象
			SecurityContext securityContext = SecurityContextHolder.getContext();
//        获取认证对象
			Authentication authentication = securityContext.getAuthentication();
			//获取User对象
			Object principal = authentication.getPrincipal();
			User user = (User) principal;
			//获取用户名
			String username = user.getUsername();
			return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Result(false, MessageConst.GET_USERNAME_FAIL);
	}

	@RequestMapping("/doSuccess")
	public String doSuccess() {
		return "redirect:http://localhost:83/pages/main.html";
	}

	@RequestMapping("/doFail")
	public String doFail() {
		return "redirect:http://localhost:83/login.html?param=loginError";
	}

	///
	/*根据条件查询分页*/
	@RequestMapping("findPage")
	@ResponseBody
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
		PageResult pageResult = userService.findPage(queryPageBean);
		return pageResult;
	}

	/*添加用户*/
	@RequestMapping("/add")
	@ResponseBody
	public Result add(Integer[] roleIds, @RequestBody SysUser sysUser) {
		try {
			userService.add(roleIds, sysUser);
			return new Result(true, MessageConst.ADD_USER_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConst.ADD_USER_FAIL);
		}
	}

	/*删除用户*/
	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id) {
		try {
			userService.deleteById(id);
			return new Result(true, MessageConst.DELETE_USER_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConst.DELETE_USER_FAIL);
		}
	}

	/*编辑中数据回显，根据id查询用户*/
	@RequestMapping("/findById")
	@ResponseBody
	public Result findById(Integer id) {
		try {
			SysUser sysUser = userService.findById(id);
			return new Result(true, MessageConst.QUERY_USER_SUCCESS, sysUser);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConst.QUERY_USER_FAIL);
		}
	}

	/*根据userId查询roleIds(即user和role的关联)*/
	@RequestMapping("/findRoleIdsByUserId")
	@ResponseBody
	public Result findRoleIdsByUserId(Integer id) {
		try {
			Integer[] roleIds = userService.findRoleIdsByUserId(id);
			return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, roleIds);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConst.QUERY_ROLE_FAIL);
		}
	}

	/*编辑用户*/
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(Integer[] roleIds, @RequestBody SysUser sysUser) {
		try {
			userService.edit(roleIds, sysUser);
			return new Result(true, MessageConst.EDIT_USER_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConst.EDIT_USER_FAIL);
		}
	}
}
